- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSolution.rs
55 lines (48 loc) · 1.51 KB
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
structMyHashMap{
buckets:Vec<Vec<(i32,i32)>>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
implMyHashMap{
/** Initialize your data structure here. */
fnnew() -> Self{
Self{
buckets:vec![Vec::new();1000],
}
}
/** value will always be non-negative. */
fnput(&mutself,key:i32,value:i32){
let id = key asusize % 1000;
matchself.buckets[id].iter().position(|&x| x.0 == key){
Some(i) => self.buckets[id][i].1 = value,
None => self.buckets[id].push((key, value)),
};
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
fnget(&self,key:i32) -> i32{
let id = key asusize % 1000;
for&(k, v)in&self.buckets[id]{
if k == key {
return v;
}
}
-1
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
fnremove(&mutself,key:i32){
let id = key asusize % 1000;
matchself.buckets[id].iter().position(|&x| x.0 == key){
Some(i) => self.buckets[id].remove(i),
None => (0,0),
};
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* let obj = MyHashMap::new();
* obj.put(key, value);
* let ret_2: i32 = obj.get(key);
* obj.remove(key);
*/